/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ejb.metrica.seguridad.dao;

import ejb.metrica.seguridad.entity.RolesDTO;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author jsanchez
 */
public class RolesDAO extends DAO{

    public Object buscar(Integer id) throws Exception {
        try {
            getConnection();
            prep = conn.prepareStatement("SELECT ID, NOMBRE, DESCRIPCION, OPCIONES FROM ROLES WHERE ID = ?");
            prep.setInt(1, id.intValue());
            resul = prep.executeQuery();
            if (resul.next()) {
                return getDatos(resul, new RolesDTO());
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            closeAll();
        }
        return null;
    }

    public Collection buscarTodos() throws Exception {
        Collection c = new ArrayList();
        try {
            getConnection();
            prep = conn.prepareStatement("SELECT ID, NOMBRE, DESCRIPCION, OPCIONES FROM ROLES");
            resul = prep.executeQuery();
            for (; resul.next(); c.add(getDatos(resul, new RolesDTO()))) {
                ;
            }
            return c;
        } catch (Exception ex) {
            throw ex;
        } finally {
            closeAll();
        }
    }

    public Integer guardar(Object obj) throws Exception {
        RolesDTO dto = (RolesDTO) obj;
        try {
            getConnection();
            if (dto.getId() == null) {
                prep = conn.prepareStatement("INSERT INTO ROLES(NOMBRE, DESCRIPCION, OPCIONES, ID) VALUES(?, ?, ?, ?)");
                dto.setId(getMaximoId("ROLES"));
            } else {
                prep = conn.prepareStatement("UPDATE ROLES SET NOMBRE=?, DESCRIPCION=?, OPCIONES=? WHERE ID=?");
            }
            setDatos(prep, dto, "NOMBRE, DESCRIPCION, OPCIONES, ID");
            prep.executeUpdate();
            return dto.getId();
        } catch (Throwable ex) {
            throw new Exception(ex);
        } finally {
            closeAll();
        }
    }

    public void eliminar(Integer id) throws Exception {
        try {
            /*conn = */ getConnection();
            prep = conn.prepareStatement("DELETE FROM ROLES WHERE ID = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (Exception ex) {
            throw ex;
        } finally {
            closeAll();
        }
    }

    public Object existe(Object obj) throws Exception {
        try {
            /*conn = */ getConnection();
            prep = conn.prepareStatement("SELECT ID FROM ROLES WHERE NOMBRE = ?");
            prep.setString(1, ((RolesDTO) obj).getNombre().trim());
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            closeAll();
        }
        return false;
    }
    
}
